const Controller = require('egg').Controller;

function toInt(str) {
    if (typeof str === 'number') return str;
    if (!str) return str;
    return parseInt(str, 10) || 0;
}
class IndexController extends Controller {
    async index() {
        const { ctx } = this;
        await ctx.render('cats');
    }
    async get() {
        const { ctx } = this;
        ctx.body = {
            code: 200,
            data: await ctx.model.Cats.findAll()
        }

    }

    async update() {
        const ctx = this.ctx;
        const id = toInt(ctx.request.body.id);
        const Cats = await ctx.model.Cats.findByPk(id);
        if (!Cats) {
            ctx.status = 404;
            return;
        }
        const { catName, pid, catImg, isShow } = ctx.request.body;
        await Cats.update({ catName, pid, catImg, isShow });
        ctx.body = Cats;
    }
    async create() {
        const ctx = this.ctx;
        console.log(ctx.request.body);
        const Cats = await ctx.model.Cats.create({ catName: ctx.request.body.catName, pid: ctx.request.body.pid,type: ctx.request.body.type });
        ctx.body = {
            code: 200,
            data: Cats
        }
    }
    async destroy() {
        const ctx = this.ctx;
        const id = toInt(ctx.params.id);
        const user = await ctx.model.Cats.findByPk(id);
        console.log(id)
        if (!user) {
            ctx.status = 404;
            return;
        }

        await user.destroy();
        ctx.status = 200;
    }

}

module.exports = IndexController;


// 1、自测
// 2、经纪人端提测  问诊表 创建 编辑  全站列表 等
// 3、患者端提测    找专家